
import axios from "axios";



/**
 * 公共方法
 */
let common = {
    baseURL: "http://119.3.43.73:8032/api/",
    radiusmultiple: 1.5,//半径倍数
    graphicLayer: null,
    initLayer(viewer) {
        // 创建矢量数据图层
        this.graphicLayer = new mars3d.layer.GraphicLayer({
            // hasEdit: true,
            // isAutoEditing: true, // 绘制完成后是否自动激活编辑
        });
        viewer.addLayer(this.graphicLayer);
        // 在layer上绑定监听事件
        // this.graphicLayer.on(mars3d.EventType.click, function (event) {
        //     console.log("监听layer，单击了矢量对象", event);
        // });
    },
    switchHorizontalOrigin(horizontalOrigin) {
        let res = Cesium.HorizontalOrigin.CENTER;
        switch (horizontalOrigin) {
            case "CENTER":
                res = Cesium.HorizontalOrigin.CENTER;//"原点在对象的水平中心"
                break;
            case "LEFT":
                res = Cesium.HorizontalOrigin.LEFT;//"原点在对象的左侧"
                break;
            case "RIGHT":
                res = Cesium.HorizontalOrigin.RIGHT;//"原点在对象的右侧"
                break;
            default:
                break;
        }
        return res;
    },
    switchVerticalOrigin(verticalOrigin) {
        let res = Cesium.VerticalOrigin.CENTER;
        switch (verticalOrigin) {
            case "CENTER":
                res = Cesium.VerticalOrigin.CENTER;//"原点在对象的垂直中心"
                break;
            case "BOTTOM":
                res = Cesium.VerticalOrigin.BOTTOM;//"原点在对象的底部"
                break;
            case "BASELINE":
                res = Cesium.VerticalOrigin.BASELINE;//"如果对象包含文本，则原点位于文本的基线，否则原点位于对象的底部"
                break;
            case "TOP":
                res = Cesium.VerticalOrigin.TOP;//"原点在对象的顶部"
                break;
            default:
                break;
        }
        return res;
    },
    switchHeightReference(heightReference) {
        let res = Cesium.HeightReference.NONE;
        switch (heightReference) {
            case "NONE":
                res = Cesium.HeightReference.NONE;//"位置绝对"
                break;
            case "CLAMP_TO_GROUND":
                res = Cesium.HeightReference.CLAMP_TO_GROUND;//"位置固定在地形上"
                break;
            case "RELATIVE_TO_GROUND":
                res = Cesium.HeightReference.RELATIVE_TO_GROUND;//"位置高度是指地形上方的高度"
                break;
            default:
                break;
        }
        return res;
    },
    //管线拐角样式
    switchCornerType(cornerType) {
        let res = null;
        switch (cornerType) {
            case "ROUNDED":
                res = Cesium.CornerType.ROUNDED; //"光滑角"
                break;
            case "MITERED":
                res = Cesium.CornerType.MITERED; //"拐角"
                break;
            case "BEVELED":
                res = Cesium.CornerType.BEVELED; //"修剪角"
                break;
            default:
                break;
        }
        return res;
    },

    //生成guid
    guid() {
        return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
            /[xy]/g,
            function (c) {
                var r = (Math.random() * 16) | 0,
                    v = c == "x" ? r : (r & 0x3) | 0x8;
                return v.toString(16);
            }
        );
    },

    //根据类型查找entity
    findEntityByType(type) {
        let entitys = [];
        let entity = viewer.entities.values;
        if (entity.length > 0) {
            entitys = entity.filter(item => item.type === type);
        }
        return entitys;
    },

    //根据名称查找entity
    findEntityByName(name) {
        let entitys = [];
        let entity = viewer.entities.values;
        if (entity.length > 0) {
            entitys = entity.filter(item => item.name === name);
        }
        return entitys;
    },
}


// setTimeout(() => {
//     common.initLayer();
// }, 1000);

function initLayer (viewer) {
    if(common.graphicLayer) return
    common.initLayer(viewer)
}

function getLayer () {
    return common.graphicLayer
}

/**
 * 获取解析arcgis的wfs数据
 */
let wfs = {
    /**
     * 解析arcgisjson数据
     * @param {*} jsonData arcgisjson数据
     * @param {*} dataType 数据类型（点、线、面）
     * @returns 
     */
    arcgisjson(jsonData, dataType) {
        var data = [];
        var length = jsonData.features.length;
        if (dataType == "点") {
            for (var i = 0; i < length; i++) {
                data.push({
                    Name: jsonData.features[i].attributes.NAME ? jsonData.features[i].attributes.NAME : jsonData.features[i].attributes.name,
                    bh: jsonData.features[i].attributes.bh,
                    MS: jsonData.features[i].attributes.MS,
                    X: jsonData.features[i].geometry.x,
                    Y: jsonData.features[i].geometry.y,
                    // Z: jsonData.features[i].attributes.MS
                });
            }
        } else if (dataType == "面") {
            for (var i = 0; i < length; i++) {
                var ringsLength = jsonData.features[i].geometry.rings.length;
                for (var k = 0; k < ringsLength; k++) {
                    var positions = [], position = [];
                    var X = 0, Y = 0;
                    var arr = jsonData.features[i].geometry.rings[k];
                    for (var j = 0; j < arr.length; j++) {
                        arr[j].splice(2, 1);
                        positions = positions.concat(arr[j]);
                        X += arr[j][0];
                        Y += arr[j][1];
                    }
                    position = { X: X / arr.length, Y: Y / arr.length }
                    data.push({
                        Name: jsonData.features[i].attributes.name ? jsonData.features[i].attributes.name : jsonData.features[i].attributes.名称,
                        // LX: jsonData.features[i].attributes.TDLX ? jsonData.features[i].attributes.TDLX : jsonData.features[i].attributes.LX,
                        positions: positions,
                        centrepoint: position
                    });
                }
            }
        } else if (dataType == "线") {
            for (var i = 0; i < length; i++) {
                var positions = [];
                var arr = jsonData.features[i].geometry.paths[0];
                let attributes = jsonData.features[i].attributes;
                for (var j = 0; j < arr.length; j++) {
                    arr[j].splice(2, 1);
                    positions = positions.concat(arr[j]);
                    // if (attributes.D_S) {
                    //     let high = random(0, 2);
                    //     positions.push(-high);
                    // }
                    if (attributes.MS) {
                        positions.push(-attributes.MS);
                    }
                }
                let radius, width, height;
                if (attributes.D_S) {
                    if (attributes.D_S.indexOf("DN") > -1) {
                        let strnum = attributes.D_S.replace("DN", "");
                        radius = strnum && strnum != "" ? Number(strnum) / 1000 / 2 : null;
                    } else if (attributes.D_S.indexOf("d") > -1) {
                        let strnum = attributes.D_S.replace("d", "");
                        radius = strnum && strnum != "" ? Number(strnum) / 1000 / 2 : null;
                    } else if (attributes.D_S.indexOf("X") > -1) {
                        let str = attributes.D_S.split("X");
                        width = str[0] ? Number(str[0]) / 1000 : null;
                        height = str[1] ? Number(str[1]) / 1000 : null;
                    } else if (attributes.D_S == " ") {
                        radius = 0.5;
                    } else {
                        radius = attributes.D_S ? Number(attributes.D_S) / 2 : null;
                        width = attributes.W ? attributes.W.toFixed(2) : null;
                        height = attributes.H ? attributes.H.toFixed(2) : null;
                    }
                }
                data.push({
                    // name: attributes.name,
                    // radius: attributes.D_S ? Number(attributes.D_S) / 2 : null,
                    // width: attributes.W ? attributes.W.toFixed(2) : null,
                    // height: attributes.H ? attributes.H.toFixed(2) : null,
                    radius: radius ? radius : null,
                    width: width ? width : null,
                    height: height ? height : null,
                    positions: positions
                });
            }
        }
        return data;
    },
    /**
     * 异步获取wfs数据，点、线、面数据
     * @param {*} url wfs连接url
     * @param {*} wfstype 数据类型（点、线、面）
     * @returns 
     */
    async getWFS(url, wfstype) {
        try {
            let res = await axios.get(url, {});
            let resData = res.data;
            if (resData) {
                let datas = arcgisjson(resData, wfstype);
                return datas;
            }
        } catch (e) {
            console.log(e);
        }
    }
}

/**
 * 符合库入数据库接口
 */
let symbol = {


    //根据符号库Id查询相关属性
    async getSymbolAttributeById(id) {
        try {
            let res = await axios.get(common.baseURL + "Symbol/GetSymbolAttributeById", {
                params: { id: id }
            });
            let attributeData = res.data;
            let attribute = JSON.parse(attributeData.attribute);
            return attribute;
        } catch (e) {
            console.log(e);
        }
    },
    //保存符号库属性
    async saveSymbolAttribute(attrData, isAdd) {
        try {
            let data = {
                Id: attrData.id,
                // Name: attrData.name,
                // SymbollevelId: attrData.symbollevelId,
                // ImageUrl: attrData.imageUrl,
                Type: attrData.type,
                TypeName: attrData.typeName,
                Attribute: attrData.attribute,
            };
            if (attrData.name) {
                data.Name = attrData.name;
            }
            if (attrData.symbollevelId) {
                data.SymbollevelId = attrData.symbollevelId;
            }
            if (attrData.imageUrl) {
                data.ImageUrl = attrData.imageUrl;
            }
            if (attrData.remarks) {
                data.Remarks = attrData.remarks;
            }
            let res = null;
            if (isAdd) {
                res = await axios.post(common.baseURL + "Symbol/AddSymbolAttribute", data);
            } else {
                res = await axios.post(common.baseURL + "Symbol/EditSymbolAttribute", data);
            }
            return res.data;
        } catch (e) {
            console.log(e);
        }
    },
    //删除符号库属性
    async deleteSymbolAttribute(ids) {
        try {
            let res = await axios.delete(common.baseURL + "Symbol/DeleteSymbolAttribute", { params: { ids: ids.join(",") } });
            return res.data;
        } catch (e) {
            console.log(e);
        }
    },

}


/**
 * DivGraphic div标签标注
 */
let divGraphic = {
    /**
     * 一个黑色面板，指向左下角黄色连线
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setBlackPanel(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "LEFT";
        attribute.verticalOrigin = attribute.verticalOrigin || "CENTER";
        let type = "div";
        // 一个黑色面板，指向左下角黄色连线
        let style = {
            html: `<div class="marsBlackPanel  animation-spaceInDown">
              <div class="marsBlackPanel-text">${attribute.text}</div>
          </div>`,
            offsetY: attribute.offsetY,
            // horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            // verticalOrigin: Cesium.VerticalOrigin.CENTER,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                400000
            ), // 按视距距离显示
            clampToGround: true,
            // 高亮时的样式
            highlight: {
                // type: mars3d.EventType.click,
                className: "marsBlackPanel-highlight",
            },
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "blackPanel",
                typeName: "黑色面板黄色连线",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    /**
     * 添加一个黑色面板，指向左下角黄色连线
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    addBlackPanel(attribute, isSave) {
        let res = this.setBlackPanel(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/2.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 根据id修改黑色面板
     * @param {*} id 
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    updateBlackPanel(id, attribute, isSave) {
        let res = this.setBlackPanel(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 一个渐变的文本面板,中间竖直连线
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setBlueGradientPnl(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || -20;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "CENTER";
        attribute.verticalOrigin = attribute.verticalOrigin || "BOTTOM";
        let type = "div";
        // 一个渐变的文本面板,中间竖直连线
        let style = {
            html: `<div class="marsBlueGradientPnl">
              <div>${attribute.text}</div>
          </div>`,
            offsetY: attribute.offsetY,
            // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                400000
            ), // 按视距距离显示

            // // 高亮时的样式
            // highlight: {
            //     type: mars3d.EventType.click,
            //     className: "marsBlueGradientPnl-highlight",
            // },
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "blueGradientPnl",
                typeName: "竖直渐变文本",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加一个渐变的文本面板,中间竖直连线
    addBlueGradientPnl(attribute, isSave) {
        let res = this.setBlueGradientPnl(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/1.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改一个渐变的文本面板,中间竖直连线
    updateBlueGradientPnl(id, attribute, isSave) {
        let res = this.setBlueGradientPnl(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 一个渐变的文本面板,中间竖虚连线
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setGreenGradientPnl(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "CENTER";
        attribute.verticalOrigin = attribute.verticalOrigin || "BOTTOM";
        let type = "div";
        // 一个渐变的文本面板,中间竖虚连线
        let style = {
            html: `<div class="marsGreenGradientPnl" >${attribute.text}</div>`,
            offsetY: attribute.offsetY,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                400000
            ), // 按视距距离显示
            // 高亮时的样式
            // highlight: {
            //   type: mars3d.EventType.click,
            //   className: "marsGreenGradientPnl-highlight"
            // }
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "greenGradientPnl",
                typeName: "竖虚渐变文本",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加竖虚渐变文本
    addGreenGradientPnl(attribute, isSave) {
        let res = this.setGreenGradientPnl(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/3.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改竖虚渐变文本
    updateGreenGradientPnl(id, attribute, isSave) {
        let res = this.setGreenGradientPnl(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * GIF图标，DIV方式
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setGif(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.url = attribute.url || "../static/images/tf.gif";
        attribute.width = attribute.width || "50";
        attribute.height = attribute.height || "40";
        attribute.horizontalOrigin = attribute.horizontalOrigin || "CENTER";
        attribute.verticalOrigin = attribute.verticalOrigin || "CENTER";
        let type = "div";
        // 添加GIF图标，DIV方式
        let style = {
            html: '<img src="' + attribute.url + '" style="width:' + attribute.width + 'px;height:' + attribute.height + 'px;" ></img>',
            offsetY: attribute.offsetY,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                200000
            ), // 按视距距离显示
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "gif",
                typeName: "GIF图标",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    /**
     * 添加GIF图标
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    addGif(attribute, isSave) {
        let res = this.setGif(attribute, isSave);
        res.attrData.imageUrl = "";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 修改GIF图标
     * @param {*} id 标识Id
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    updateGif(id, attribute, isSave) {
        let res = this.setGif(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 内置扩展的动态文本
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setBoderLabel(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.font_size = attribute.font_size || 15;
        attribute.font_family = attribute.font_family || "微软雅黑";
        attribute.color = attribute.color || "#ccc";
        attribute.borderColor = attribute.borderColor || "#15d1f2";
        let type = "divBoderLabel";
        //内置扩展的动态文本
        let style = {
            offsetY: attribute.offsetY,
            text: attribute.text,
            font_size: attribute.font_size,
            font_family: attribute.font_family,
            color: attribute.color,
            borderColor: attribute.borderColor,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                400000
            ), // 按视距距离显示
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "boderLabel",
                typeName: "动态文本",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加动态文本
    addBoderLabel(attribute, isSave) {
        let res = this.setBoderLabel(attribute, isSave);
        res.attrData.imageUrl = "../../../public/images/lable/4.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改动态文本
    updateBoderLabel(id, attribute, isSave) {
        let res = this.setBoderLabel(attribute, isSave);
        // this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
        this.updateGraphicAdd(id, res.type, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 内置扩展的竖立文本
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setUpLabel(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.color = attribute.color || "#fff";
        attribute.font_size = attribute.font_size || 16;
        attribute.font_family = attribute.font_family || "微软雅黑";
        attribute.lineHeight = attribute.lineHeight || 60;
        attribute.circleSize = attribute.circleSize || 8;
        let type = "divUpLabel";
        //内置扩展的竖立文本
        let style = {
            offsetY: attribute.offsetY,
            text: attribute.text,
            color: attribute.color,
            font_size: attribute.font_size,
            font_family: attribute.font_family,
            lineHeight: attribute.lineHeight,
            circleSize: attribute.circleSize,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                400000
            ), // 按视距距离显示
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "upLabel",
                typeName: "竖立文本",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加竖立文本
    addUpLabel(attribute, isSave) {
        let res = this.setUpLabel(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/8.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改竖立文本
    updateUpLabel(id, attribute, isSave) {
        let res = this.setUpLabel(attribute, isSave);
        // this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
        this.updateGraphicAdd(id, res.type, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 蓝色科技感文本面板
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setSpot(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || -25;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "CENTER";
        attribute.verticalOrigin = attribute.verticalOrigin || "BOTTOM";
        let type = "div";
        // 蓝色科技感文本面板
        let style = {
            html: `<div class="mars-spot">
                    <div class="mars-spot-board">
                    <h5>${attribute.text}</h5>
                    </div>
                    <div class="mars-spot-line"></div>
                </div>`,
            offsetY: attribute.offsetY,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // verticalOrigin: Cesium.VerticalOrigin.BTOOM
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "spot",
                typeName: "蓝色文本面板",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加蓝色文本面板
    addSpot(attribute, isSave) {
        let res = this.setSpot(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/emergency.gif";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改蓝色文本面板
    updateSpot(id, attribute, isSave) {
        let res = this.setSpot(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },

    /**
     * 加css动画的扩散点
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setLightPoint(attribute, isSave) {
        let position = attribute.position;
        attribute.color = attribute.color || "#f33349";
        attribute.offsetY = attribute.offsetY || 38;
        attribute.heightReference = attribute.heightReference || "CLAMP_TO_GROUND";
        let type = "divLightPoint";
        let style = {
            offsetY: attribute.offsetY,
            color: attribute.color,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                400000
            ), // 按视距距离显示
            heightReference: common.switchHeightReference(attribute.heightReference),
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "lightPoint",
                typeName: "扩散点",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加扩散点
    addLightPoint(attribute, isSave) {
        let res = this.setLightPoint(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/emergency.gif";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改扩散点
    updateLightPoint(id, attribute, isSave) {
        let res = this.setLightPoint(attribute, isSave);
        // this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
        this.updateGraphicAdd(id, res.type, res.style, res.position, res.isSave, res.attrData);
    },
    /**
     * 倾斜指向左下角的面板样式，只有标题
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库 
     */
    setImgPanel1(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "LEFT";
        attribute.verticalOrigin = attribute.verticalOrigin || "BOTTOM";
        attribute.clampToGround = attribute.clampToGround || true;
        let type = "div";
        //面板
        let style = {
            html: '<div class="marsImgPanel1"><div class="title">' + attribute.title + '</div></div>',
            offsetY: attribute.offsetY,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            clampToGround: attribute.clampToGround,
            // scale: 1.0,
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "imgPanel1",
                typeName: "图片面板1",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加图片面板1
    addImgPanel1(attribute, isSave) {
        let res = this.setImgPanel1(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/div1.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改图片面板1
    updateImgPanel1(id, attribute, isSave) {
        let res = this.setImgPanel1(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },

    /**
     * 倾斜指向左下角的面板样式,带标题和内容
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    setImgPanel2(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "LEFT";
        attribute.verticalOrigin = attribute.verticalOrigin || "BOTTOM";
        let type = "div";
        //面板
        let style = {
            html: `<div class="marsImgPanel2">
                    <div class="title">${attribute.title}</div>
                    <div class="content">${attribute.content}</div>
                </div>`,
            offsetY: attribute.offsetY,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                200000
            ), // 按视距距离显示
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "imgPanel2",
                typeName: "图片面板2",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },

    //添加图片面板2
    addImgPanel2(attribute, isSave) {
        let res = this.setImgPanel2(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/div2.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改图片面板2
    updateImgPanel2(id, attribute, isSave) {
        let res = this.setImgPanel2(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },

    /**
     * 倾斜指向左下角的面板样式
     * @param {*} attribute 属性对象
     * {    position: 位置,
            color: 颜色(蓝色:blue,绿色:green,红色:red,黄色:e9b709),
            title: 标题,
            content: 内容,
            offsetY: 38,//偏移
            horizontalOrigin: "LEFT",//"CENTER": "原点在对象的水平中心" ,LEFT: "原点在对象的左侧" ,"RIGHT": "原点在对象的右侧" 
            verticalOrigin: "BOTTOM",//"CENTER", label: "原点在对象的垂直中心" ,"BOTTOM": "原点在对象的底部" ,"BASELINE": "如果对象包含文本，则原点位于文本的基线，否则原点位于对象的底部","TOP": "原点在对象的顶部" }
        }
     * @param {*} isSave 是否保存数据库
     */
    setTiltPanel(attribute, isSave) {
        let position = attribute.position;
        attribute.offsetY = attribute.offsetY || 38;
        attribute.horizontalOrigin = attribute.horizontalOrigin || "LEFT";
        attribute.verticalOrigin = attribute.verticalOrigin || "BOTTOM";
        attribute.clampToGround = attribute.clampToGround || true;
        attribute.color = attribute.color || "blue";
        attribute.content = attribute.content || "";
        // `<div class="data-li">
        //     <div class="data-label">实时流量：</div>
        //     <div class="data-value"><span class="label-num">99</span><span class="label-unit">m³/s</span>
        //     </div>
        // </div>
        // <div class="data-li">
        //     <div class="data-label">出水阀门：</div>
        //     <div class="data-value"><span class="label-tag data-value-status-1" alt="中间状态">1号</span><span
        //             class="label-tag data-value-status-2" alt="打开状态">2号</span></div>
        // </div>`
        let type = "div";
        let style = {
            html: `<div class="marsTiltPanel marsTiltPanel-theme-${attribute.color}">
                <div class="marsTiltPanel-wrap">
                    <div class="area">
                        <div class="arrow-lt"></div>
                        <div class="b-t"></div>
                        <div class="b-r"></div>
                        <div class="b-b"></div>
                        <div class="b-l"></div>
                        <div class="arrow-rb"></div>
                        <div class="label-wrap">
                            <div class="title">${attribute.title}</div>
                            <div class="label-content">
                                ${attribute.content}
                            </div>
                        </div>
                    </div>
                    <div class="b-t-l"></div>
                    <div class="b-b-r"></div>
                </div>
                <div class="arrow" ></div>
            </div>`,
            offsetY: attribute.offsetY,
            horizontalOrigin: common.switchHorizontalOrigin(attribute.horizontalOrigin),
            verticalOrigin: common.switchVerticalOrigin(attribute.verticalOrigin),
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                200000
            ), // 按视距距离显示
            scaleByDistance: new Cesium.NearFarScalar(1000, 1.0, 200000, 0.2),
            clampToGround: attribute.clampToGround,
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.position = JSON.stringify(position);
            attrData = {
                type: "tiltPanel",
                typeName: "倾斜面板",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, position: position, isSave: isSave, attrData: attrData };
    },
    //添加倾斜面板
    addTiltPanel(attribute, isSave) {
        let res = this.setTiltPanel(attribute, isSave);
        res.attrData.imageUrl = "../static/images/lable/7.png";
        this.addGraphic(res.type, res.style, res.position, res.isSave, res.attrData);
    },
    //修改倾斜面板
    updateTiltPanel(id, attribute, isSave) {
        let res = this.setTiltPanel(attribute, isSave);
        this.updateGraphic(id, res.style, res.position, res.isSave, res.attrData);
    },

    /**
     * 根据类型添加div标注
     * @param {*} type 类型{blackPanel,blueGradientPnl,greenGradientPnl,gif,boderLabel,upLabel,spot,lightPoint,imgPanel1,imgPanel2,tiltPanel}
     * @param {*} attribute 
     * {    position: 位置,
            text: 文本,
            font_size: 字体大小,
            font_family: 字体,
            borderColor: 边框颜色,
            url: GIF图片路径,
            width: GIF图片宽度,
            height: GIF图片高度,
            lineHeight: 竖立文本线高,
            circleSize: 竖立文本原点大小,
            color: 颜色 类型为tiltPanel：(蓝色:blue,绿色:green,红色:red,黄色:e9b709),
            title: 标题,
            content: 内容,
            offsetY: 38,//偏移
            horizontalOrigin: "LEFT",//"CENTER": "原点在对象的水平中心" ,LEFT: "原点在对象的左侧" ,"RIGHT": "原点在对象的右侧" 
            verticalOrigin: "BOTTOM",//"CENTER", label: "原点在对象的垂直中心" ,"BOTTOM": "原点在对象的底部" ,"BASELINE": "如果对象包含文本，则原点位于文本的基线，否则原点位于对象的底部","TOP": "原点在对象的顶部" }
        }
     * @param {*} isSave 是否保存数据库
     */
    addGraphicByType(type, attribute, isSave) {
        isSave = isSave || false;
        switch (type) {
            case "blackPanel":   //一个黑色面板，指向左下角黄色连线
                this.addBlackPanel(attribute, isSave);
                break;
            case "blueGradientPnl":   //一个渐变的文本面板,中间竖直连线
                this.addBlueGradientPnl(attribute, isSave);
                break;
            case "greenGradientPnl":    //一个渐变的文本面板,中间竖虚连线
                this.addGreenGradientPnl(attribute, isSave);
                break;
            case "gif":         //添加GIF图标，DIV方式
                this.addGif(attribute, isSave);
                break;
            case "boderLabel":  //内置扩展的动态文本
                this.addBoderLabel(attribute, isSave);
                break;
            case "upLabel":     //内置扩展的竖立文本
                this.addUpLabel(attribute, isSave);
                break;
            case "spot":        //蓝色科技感文本面板
                this.addSpot(attribute, isSave);
                break;
            case "lightPoint":  //加css动画的扩散点
                this.addLightPoint(attribute, isSave);
                break;
            case "imgPanel1":   //倾斜指向左下角的面板样式，只有标题
                this.addImgPanel1(attribute, isSave);
                break;
            case "imgPanel2":   //倾斜指向左下角的面板样式,带标题和内容
                this.addImgPanel2(attribute, isSave);
                break;
            case "tiltPanel":   //倾斜指向左下角的面板样式
                this.addTiltPanel(attribute, isSave);
                break;
            default:
                break;
        }

    },

    symbolId: null,
    addGraphicById(id, type, attribute, isSave) {
        isSave = isSave || false;
        this.symbolId = id;
        switch (type) {
            case "blackPanel":   //一个黑色面板，指向左下角黄色连线
                this.addBlackPanel(attribute, isSave);
                break;
            case "blueGradientPnl":   //一个渐变的文本面板,中间竖直连线
                this.addBlueGradientPnl(attribute, isSave);
                break;
            case "greenGradientPnl":    //一个渐变的文本面板,中间竖虚连线
                this.addGreenGradientPnl(attribute, isSave);
                break;
            case "gif":         //添加GIF图标，DIV方式
                this.addGif(attribute, isSave);
                break;
            case "boderLabel":  //内置扩展的动态文本
                this.addBoderLabel(attribute, isSave);
                break;
            case "upLabel":     //内置扩展的竖立文本
                this.addUpLabel(attribute, isSave);
                break;
            case "spot":        //蓝色科技感文本面板
                this.addSpot(attribute, isSave);
                break;
            case "lightPoint":  //加css动画的扩散点
                this.addLightPoint(attribute, isSave);
                break;
            case "imgPanel1":   //倾斜指向左下角的面板样式，只有标题
                this.addImgPanel1(attribute, isSave);
                break;
            case "imgPanel2":   //倾斜指向左下角的面板样式,带标题和内容
                this.addImgPanel2(attribute, isSave);
                break;
            case "tiltPanel":   //倾斜指向左下角的面板样式
                this.addTiltPanel(attribute, isSave);
                break;
            default:
                break;
        }

    },

    /**
     * 根据类型修改div标注
     * @param {*} type 类型
     * @param {*} id 标识id
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    updateGraphicByType(type, id, attribute, isSave) {
        isSave = isSave || false;
        switch (type) {
            case "blackPanel":   //一个黑色面板，指向左下角黄色连线
                this.updateBlackPanel(id, attribute, isSave);
                break;
            case "blueGradientPnl":   //一个渐变的文本面板,中间竖直连线
                this.updateBlueGradientPnl(id, attribute, isSave);
                break;
            case "greenGradientPnl":    //一个渐变的文本面板,中间竖虚连线
                this.updateGreenGradientPnl(id, attribute, isSave);
                break;
            case "gif":         //添加GIF图标，DIV方式
                this.updateGif(id, attribute, isSave);
                break;
            case "boderLabel":  //内置扩展的动态文本
                this.updateBoderLabel(id, attribute, isSave);
                break;
            case "upLabel":     //内置扩展的竖立文本
                this.updateUpLabel(id, attribute, isSave);
                break;
            case "spot":        //蓝色科技感文本面板
                this.updateSpot(id, attribute, isSave);
                break;
            case "lightPoint":  //加css动画的扩散点
                this.updateLightPoint(id, attribute, isSave);
                break;
            case "imgPanel1":   //倾斜指向左下角的面板样式，只有标题
                this.updateImgPanel1(id, attribute, isSave);
                break;
            case "imgPanel2":   //倾斜指向左下角的面板样式,带标题和内容
                this.updateImgPanel2(id, attribute, isSave);
                break;
            case "tiltPanel":   //倾斜指向左下角的面板样式
                this.updateTiltPanel(id, attribute, isSave);
                break;
            default:
                break;
        }
    },

    //添加div标注图层
    addGraphic(type, style, position, isSave, attrData) {
        // let id = attribute.hasOwnProperty("id") ? attrData.id : common.guid();
        let id = this.symbolId ? this.symbolId : common.guid();
        let graphic = mars3d.GraphicUtil.create(type, {
            id: id,
            position: position,
            style: style,
        })
        common.graphicLayer.addGraphic(graphic);
        if (isSave) {
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, true);
        }
        this.symbolId = null;
    },

    //修改div标注
    updateGraphic(id, newStyle, position, isSave, attrData) {
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            graphic.setStyle(newStyle);
            if (position) {
                graphic.position = position;
            }
        }
        if (isSave) {
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, false);
        }
    },
    //更新div标注(删除旧添加新)
    updateGraphicAdd(id, type, style, position, isSave, attrData) {
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            common.graphicLayer.removeGraphic(graphic);
            graphic = mars3d.GraphicUtil.create(type, {
                id: id,
                position: position,
                style: style,
            })
            common.graphicLayer.addGraphic(graphic);
        }
        if (isSave) {
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, false);
        }
    },
    /**
     * 根据id重新赋值参数
     * @param {*} id 
     * @param {*} options 
     */
    setOptions(id, options) {
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            graphic.setOptions(options);
        }
    },
    /**
     * 根据id设置样式信息
     * @param {*} id 
     * @param {*} newStyle 新样式
     */
    setStyle(id, newStyle) {
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            graphic.setStyle(newStyle);
        }
    },
    //开始绘制标注
    startDrawGraphic(type, style) {
        // 开始绘制
        common.graphicLayer.startDraw({
            type: type,
            style: style,
        });
    },
    //删除图层矢量数据
    removeGraphic(graphic) {
        common.graphicLayer.removeGraphic(graphic);
    },
    //根据id删除图层矢量数据
    removeDivGraphic(id, isRemove) {
        isRemove = isRemove || false;
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            common.graphicLayer.removeGraphic(graphic);
        }
        if (isRemove) {
            symbol.deleteSymbolAttribute([id]);
        }
    },
    //删除所有的div标注矢量
    clearAllDivGraphic(isRemove) {
        isRemove = isRemove || false;
        let ids = [];
        // let graphics = this.graphicLayer.getGraphicsByAttr("name", "div");
        // let graphics = common.graphicLayer.getGraphicsByAttr("name", "div");
        let graphics = common.graphicLayer.getGraphicsByAttr("div", "type");
        for (let i = 0; i < graphics.length; i++) {
            // this.graphicLayer.removeGraphic(graphics[i]);
            common.graphicLayer.removeGraphic(graphics[i]);
            ids.push(graphics[i].id);
        }
        if (isRemove) {
            symbol.deleteSymbolAttribute(ids);
        }
    },
}

/**
 * 管网
 */
let pipe = {

    //添加管线带线
    addPolylineVolumeLine: function (id, name, type, positions, shape, linewidth, color, cornerType) {
        // let positions = data.positions;
        // let shape = null;

        // let material = new Cesium.PolylineTrailMaterialProperty({
        //     //创建材质
        //     color: Cesium.Color.fromCssColorString(color), //Cesium.Color.RED.withAlpha(0.5),
        //     duration: 6000, //3000
        //     ratio: 2.0,
        //     trailImage: require("../assets/images/lineClr.png")
        // });
        let entity = viewer.entities.add({
            id: id,
            name: name,
            type: type,
            show: true,
            polylineVolume: {
                // positions: positions,
                // positions: Cesium.Cartesian3.fromDegreesArray(positions),//[经度，纬度，经度，纬度...]
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),//[经度，纬度，高度，经度，纬度，高度...]
                shape: shape,
                // material: Cesium.Color.RED,
                material: Cesium.Color.fromCssColorString(color),
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 500.0),
            },
            polyline: {
                // positions: positions,
                // positions: Cesium.Cartesian3.fromDegreesArray(positions),//[经度，纬度，经度，纬度...]
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),
                width: linewidth,
                material: Cesium.Color.fromCssColorString(color),
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(500.0, 200000.0),
                // clampToGround: true
                //arcType: Cesium.ArcType.GEODESIC
            },
        });
        if (cornerType != null) {
            entity.polylineVolume.cornerType = cornerType;
        }
    },

    //井
    addWell(name, type, positions, radius) {
        let length = radius * 2;
        let height = radius / common.radiusmultiple;
        for (let i = 1; i < positions.length - 1; i++) {
            if (positions[i][2]) {
                height = Number(positions[i][2]) / 2 - 0.2;
                length = Number(positions[i][2]) + 0.5;
            }
            var entity = viewer.entities.add({
                name: name,
                type: type,//自定义属性
                position: Cesium.Cartesian3.fromDegrees(positions[i][0], positions[i][1], height),
                cylinder: {
                    length: length,
                    topRadius: radius,
                    bottomRadius: radius,
                    material: new Cesium.ImageMaterialProperty({
                        image: require("../assets/images/icon_well.png"),
                        repeat: new Cesium.Cartesian2(1.0, 1.0),
                    }),
                    distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                        0.0,
                        500.0
                    ),
                    // outline: true,
                    // outlineColor: Cesium.Color.DARK_GREEN,
                },
            });
        }
    },

    //添加管线带线，接头样式是井
    addPolylineVolumeLineWell: function (name, type, positions, shape, linewidth, color) {
        for (let i = 0; i < positions.length; i++) {
            this.addPolylineVolumeLine(null, name, type, positions[i], shape, linewidth, color);
        }
    },

    //管道形状【方】
    computeSquare(width, height) {
        return [
            new Cesium.Cartesian2(-(width / 2), -(height / 2)),
            new Cesium.Cartesian2(width / 2, -(height / 2)),
            new Cesium.Cartesian2(width / 2, height / 2),
            new Cesium.Cartesian2(-(width / 2), (height / 2)),
        ];
    },
    //管道形状【内空管道】 radius整个管道的外半径
    computeKxCircle(radius) {
        let hd = radius / 3;
        let startAngle = 0;
        let endAngle = 360;

        let pss = [];
        for (let i = startAngle; i <= endAngle; i++) {
            var radians = Cesium.Math.toRadians(i);
            pss.push(
                new Cesium.Cartesian2(
                    radius * Math.cos(radians),
                    radius * Math.sin(radians)
                )
            );
        }
        for (let i = endAngle; i >= startAngle; i--) {
            let radians = Cesium.Math.toRadians(i);
            pss.push(
                new Cesium.Cartesian2(
                    (radius - hd) * Math.cos(radians),
                    (radius - hd) * Math.sin(radians)
                )
            );
        }
        return pss;
    },
    //管道形状【圆柱体】 radius整个管道的外半径
    computeCircle(radius) {
        let positions = [];
        for (let i = 0; i < 360; i++) {
            let radians = Cesium.Math.toRadians(i);
            positions.push(
                new Cesium.Cartesian2(
                    radius * Math.cos(radians),
                    radius * Math.sin(radians)
                )
            );
        }
        return positions;
    },
    //管道形状【星状】 radius整个管道的外半径 ,arms星角的个数
    computeStar(arms, rOuter, rInner) {
        let angle = Math.PI / arms;
        let length = 2 * arms;
        let positions = new Array(length);
        for (let i = 0; i < length; i++) {
            let r = i % 2 === 0 ? rOuter : rInner;
            positions[i] = new Cesium.Cartesian2(
                Math.cos(i * angle) * r,
                Math.sin(i * angle) * r
            );
        }
        return positions;
    },
    //三角形
    computeTriangle(radius) {
        var jiaodu = 60;
        var hudu = Cesium.Math.toRadians(jiaodu);
        var positions = [];
        positions.push(new Cesium.Cartesian2(-radius * Math.cos(hudu), -radius * Math.sin(hudu) / 2));
        positions.push(new Cesium.Cartesian2(radius * Math.cos(hudu), -radius * Math.sin(hudu) / 2));
        positions.push(new Cesium.Cartesian2(0, radius * Math.sin(hudu) / 2));
        return positions;
    },

    //获取管线形状
    getShape(attribute) {
        let shape = null;
        if (attribute.pipelineType == "circle") {//"圆" 
            shape = this.computeCircle(attribute.radius);
        } else if (attribute.pipelineType == "square") {//"方" 
            shape = this.computeSquare(attribute.width, attribute.height)
        } else if (attribute.pipelineType == "pipeline") {//"内空圆"
            shape = this.computeKxCircle(attribute.radius)
        } else if (attribute.pipelineType == "star") {//"星" 参数：个数,外半径,内半径
            shape = this.computeStar(attribute.arms, attribute.rOuter, attribute.rInner);
        }
        return shape;
    },

    //获取位置高
    getWellPositions(positions) {
        let res = [];
        for (let i = 0; i < positions.length;) {
            res.push([Number(positions[i]), Number(positions[i + 1]), Number(positions[i + 2])]);
            i = i + 3;
        }
        return res;
    },

    /**
     * 添加管线
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    addPipeline(attribute, isSave) {
        let type = "pipeline";
        let id = attribute.hasOwnProperty("id") ? attribute.id : common.guid();
        let positions = attribute.positions;//[经,纬,高,经,纬,高]
        attribute.pipelineType = attribute.pipelineType || "circle";//"square": "方","circle": "圆" ,"pipeline":"内空圆","star": "星"
        attribute.cornerType = attribute.cornerType || "ROUNDED";// "ROUNDED": "光滑角","MITERED": "拐角","BEVELED": "修剪角","well": "井"
        attribute.radius = attribute.radius || 0.5;
        if (attribute.pipelineType == "square") {//"方" 
            attribute.width = attribute.width || 0.5;
            attribute.height = attribute.height || 0.5;
        } else if (attribute.pipelineType == "star") {//"星" 参数：个数,外半径,内半径
            attribute.arms = attribute.arms || 5;
            attribute.rOuter = attribute.rOuter || 0.5;
            attribute.rInner = attribute.rInner || 0.4;
        }
        let shape = this.getShape(attribute);
        if (!shape) {
            shape = this.computeCircle(attribute.radius);
        }
        attribute.color = attribute.color || "#409EFF";
        attribute.linewidth = attribute.linewidth || 2;
        attribute.type = type;
        this.addPolylineVolumeLine(id, "", attribute.type, positions, shape, attribute.linewidth, attribute.color, cornerType);
        let cornerType = common.switchCornerType(attribute.cornerType);
        if (cornerType == null) {
            let radius = attribute.radius * common.radiusmultiple;
            let pos = this.getWellPositions(positions);
            this.addWell(id, type, pos, radius);
        }
        let attrData = {};
        if (isSave) {
            attribute.positions = JSON.stringify(positions);
            attrData = {
                type: "pipeline",
                typeName: "管线",
                attribute: JSON.stringify(attribute),
            };
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, true);
        }


    },

    /**
     * 修改管线
     * @param {*} id 标识Id
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    updatePipeline(id, attribute, isSave) {
        attribute.pipelineType = attribute.pipelineType || "circle";//"square": "方","circle": "圆" ,"pipeline":"内空圆","star": "星"
        attribute.cornerType = attribute.cornerType || "ROUNDED";// "ROUNDED": "光滑角","MITERED": "拐角","BEVELED": "修剪角","well": "井"
        attribute.radius = attribute.radius || 0.5;
        attribute.color = attribute.color || "#409EFF";
        attribute.linewidth = attribute.linewidth || 2;
        let entity = viewer.entities.getById(id);
        if (entity) {
            if (attribute.positions) {
                entity.polyline.positions = Cesium.Cartesian3.fromDegreesArrayHeights(attribute.positions);
                entity.polylineVolume.positions = Cesium.Cartesian3.fromDegreesArrayHeights(attribute.positions);
            }
            if (attribute.color) {
                entity.polyline.material = Cesium.Color.fromCssColorString(attribute.color);
                entity.polylineVolume.material = Cesium.Color.fromCssColorString(attribute.color);
            }
            if (attribute.pipelineType) {
                if (attribute.pipelineType == "square") {//"方" 
                    attribute.width = attribute.width || 0.5;
                    attribute.height = attribute.height || 0.5;
                } else if (attribute.pipelineType == "star") {//"星" 参数：个数,外半径,内半径
                    attribute.arms = attribute.arms || 5;
                    attribute.rOuter = attribute.rOuter || 0.5;
                    attribute.rInner = attribute.rInner || 0.4;
                }
                let shape = this.getShape(attribute);
                if (shape) {
                    entity.polylineVolume.shape = shape;
                }
            }
            if (attribute.cornerType) {
                let entitys = common.findEntityByName(id);
                for (let i = 0; i < entitys.length; i++) {
                    viewer.entities.remove(entitys[i]);
                }
                let cornerType = common.switchCornerType(attribute.cornerType);
                if (cornerType != null) {
                    entity.polylineVolume.cornerType = cornerType;
                } else {
                    let radius = attribute.radius * common.radiusmultiple;
                    let pos = this.getWellPositions(attribute.positions);
                    this.addWell(id, "pipeline", pos, radius);
                }
            }
            if (attribute.linewidth) {
                entity.polyline.width = attribute.linewidth;
            }
        }

        if (isSave) {
            attribute.positions = JSON.stringify(attribute.positions);
            let attrData = {
                type: "pipeline",
                typeName: "管线",
                attribute: JSON.stringify(attribute),
            };
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, false);
        }
    },

    /**
     * 根据id删除管线
     * @param {*} id 
     */
    removePipeline(id, isRemove) {
        isRemove = isRemove || false;
        viewer.entities.removeById(id);
        let entitys = common.findEntityByName(id);
        for (let i = 0; i < entitys.length; i++) {
            viewer.entities.remove(entitys[i]);
        }
        isRemove = isRemove || false;
        if (isRemove) {
            symbol.deleteSymbolAttribute([id]);
        }
    },

    /**
     * 删除所有管线
     */
    clearAllPipeline(isRemove) {
        isRemove = isRemove || false;
        let ids = [];
        let entitys = common.findEntityByType("pipeline");
        for (let i = 0; i < entitys.length; i++) {
            viewer.entities.remove(entitys[i]);
            ids.push(entitys[i].id);
        }
        if (isRemove) {
            symbol.deleteSymbolAttribute(ids);
        }
    },

}

/**
 * 公路
 */
let road = {
    //方形管道
    addPolylineVolume(id, type, positions, width) {
        viewer.entities.add({
            id: id,
            type: type,
            show: true,
            polylineVolume: {
                // positions: positions, //笛卡尔坐标
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),//[经度，纬度，高度，经度，纬度，高度...]
                shape: this.computeFang(width, 0.25),
                // cornerType: Cesium.CornerType.BEVELED,//拐角的样式:ROUNDED角有光滑的边缘,MITERED拐角点是相邻边的交点,BEVELED角被修剪
                material: Cesium.Color.fromCssColorString("#333333"),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,//贴地
                disableDepthTestDistance: 500000,//500km以内不受深度测试影响
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                    0.0,
                    50000.0
                ),
                // outline: true,
                // outlineColor: Cesium.Color.BLACK,
            },
        });
        viewer.entities.add({
            id: id + "隔离带",
            type: type,
            show: true,
            polylineVolume: {
                // positions: positions, //笛卡尔坐标
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),//[经度，纬度，高度，经度，纬度，高度...]
                shape: this.computeFang(0.5, 1),
                // cornerType: Cesium.CornerType.BEVELED,//拐角的样式:ROUNDED角有光滑的边缘,MITERED拐角点是相邻边的交点,BEVELED角被修剪
                material: Cesium.Color.fromCssColorString("white"),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,//贴地
                disableDepthTestDistance: 500000,//500km以内不受深度测试影响
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                    0.0,
                    50000.0
                ),
                // outline: true,
                // outlineColor: Cesium.Color.BLACK,
            },
        });
    },
    //方形
    computeFang(width, height) {
        var positions = [];
        positions.push(new Cesium.Cartesian2(-(width / 2), -(height / 2)));
        positions.push(new Cesium.Cartesian2(width / 2, -(height / 2)));
        positions.push(new Cesium.Cartesian2(width / 2, height / 2));
        positions.push(new Cesium.Cartesian2(-(width / 2), (height / 2)));
        return positions;
    },

    /**
     * 添加公路
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    addRoad(attribute, isSave) {
        let type = "road";
        let id = attribute.id || common.guid();
        let positions = attribute.positions;//[经,纬,高,经,纬,高]
        // attribute.cornerType = attribute.cornerType || "ROUNDED";// "ROUNDED": "光滑角","MITERED": "拐角","BEVELED": "修剪角","well": "井"
        attribute.width = attribute.width || 10;
        attribute.type = type;

        this.addPolylineVolume(id, type, positions, attribute.width);
        let attrData = {};
        if (isSave) {
            attribute.positions = JSON.stringify(positions);
            attrData = {
                type: "road",
                typeName: "公路",
                attribute: JSON.stringify(attribute),
            };
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, true);
        }

    },

    /**
     * 更新公路
     * @param {*} id 
     * @param {*} attribute 属性对象
     * @param {*} isSave 是否保存数据库
     */
    updateRoad(id, attribute, isSave) {
        attribute.width = attribute.width || 10;
        attribute.type = "road";
        let entity = viewer.entities.getById(id);
        if (entity) {
            if (attribute.positions) {
                entity.polylineVolume.positions = Cesium.Cartesian3.fromDegreesArrayHeights(attribute.positions);
                let gentity = viewer.entities.getById(id + "隔离带");
                if (gentity) {
                    gentity.polylineVolume.positions = Cesium.Cartesian3.fromDegreesArrayHeights(attribute.positions);
                }
            }
            if (attribute.width) {
                entity.polylineVolume.shape = this.computeFang(attribute.width, 0.25);
            }
        }
        if (isSave) {
            attribute.positions = JSON.stringify(attribute.positions);
            let attrData = {
                type: "road",
                typeName: "公路",
                attribute: JSON.stringify(attribute),
            };
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, false);
        }
    },

    /**
     * 根据Id删除公路
     * @param {*} id 
     */
    removeRoad(id, isRemove) {
        isRemove = isRemove || false;
        viewer.entities.removeById(id);
        viewer.entities.removeById(id + "隔离带");
        if (isRemove) {
            symbol.deleteSymbolAttribute([id]);
        }
    },

    /**
     * 删除所有公路
     */
    clearAllRoad(isRemove) {
        isRemove = isRemove || false;
        let ids = [];
        let entitys = common.findEntityByType("road");
        for (let i = 0; i < entitys.length; i++) {
            viewer.entities.remove(entitys[i]);
            ids.push(entitys[i].id);
        }
        if (isRemove) {
            symbol.deleteSymbolAttribute(ids);
        }
    },
}

/**
 * 围墙
 */
let wall = {
    symbolId: null,
    //添加围墙图层
    addGraphic(type, style, positions, isSave, attrData) {
        let id = this.symbolId ? this.symbolId : common.guid();
        let graphic = mars3d.GraphicUtil.create(type, {
            id: id,
            positions: positions,
            style: style,
            attr: { name: "wall" }
        })
        common.graphicLayer.addGraphic(graphic);
        if (isSave) {
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, true);
        }
        this.symbolId = null;
    },

    //修改围墙
    updateGraphic(id, newStyle, positions, isSave, attrData) {
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            graphic.setStyle(newStyle);
            if (positions) {
                graphic.positions = positions;
            }
        }
        if (isSave) {
            attrData.id = id;
            symbol.saveSymbolAttribute(attrData, false);
        }
    },
    //设置围墙属性
    setWall(attribute, isSave) {
        let positions = attribute.positions;
        attribute.closure = attribute.closure || false;//是否闭合
        attribute.diffHeight = attribute.diffHeight || 100;//墙高
        attribute.image = attribute.image || require("../assets/images/fence.png");//背景图片URL
        attribute.color = attribute.color || "#00ffff";//背景图片颜色
        attribute.repeat = attribute.repeat || 1;//重复次数
        attribute.speed = attribute.speed || 10;//速度，值越大越快
        attribute.axisY = attribute.axisY || false;//方向，true时上下，false左右
        attribute.hasImage2 = attribute.hasImage2 || false;//是否有2张图片的混合模式
        attribute.image2 = attribute.image2 || "";//第2张背景图片URL地址
        attribute.color2 = attribute.color2 || "";//第2张背景图片颜色
        let repeat = new Cesium.Cartesian2(1, 1);//横纵方向重复次数
        if (attribute.axisY) {
            repeat = new Cesium.Cartesian2(1, attribute.repeat);
        } else {
            repeat = new Cesium.Cartesian2(attribute.repeat, 1);
        }
        let type = "wall";
        //面板
        let style = {
            closure: attribute.closure,
            diffHeight: attribute.diffHeight,//墙高
            //动画线材质
            material: mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.LineFlow, {
                image: attribute.image,//背景图片URL
                color: attribute.color,// Cesium.Color.CHARTREUSE,//背景图片颜色
                repeat: repeat, //横纵方向重复次数
                speed: attribute.speed,//速度，值越大越快
                axisY: attribute.axisY,//方向，true时上下，false左右
                hasImage2: attribute.hasImage2,
                image2: attribute.image2,
                color2: attribute.color2,
            }),
        };
        attribute.type = type;
        let attrData = {};
        if (isSave) {
            attribute.positions = JSON.stringify(positions);
            attrData = {
                type: "wall",
                typeName: "围墙",
                attribute: JSON.stringify(attribute),
            };
        }
        return { type: type, style: style, positions: positions, isSave: isSave, attrData: attrData };
    },

    /**
     * 添加围墙
     * @param {*} attribute 
     * @param {*} isSave 
     */
    addWall(attribute, isSave) {
        let res = this.setWall(attribute, isSave);
        this.addGraphic(res.type, res.style, res.positions, res.isSave, res.attrData);
    },

    /**
     * 修改围墙
     * @param {*} id 
     * @param {*} attribute 
     * @param {*} isSave 
     */
    updateWall(id, attribute, isSave) {
        let res = this.setWall(attribute, isSave);
        this.updateGraphic(id, res.style, res.positions, res.isSave, res.attrData);
    },

    //根据id添加围墙
    addWallById(id, attribute, isSave) {
        isSave = isSave || false;
        this.symbolId = id;
        let res = this.setWall(attribute, isSave);
        this.addGraphic(res.type, res.style, res.positions, res.isSave, res.attrData);
    },

    /**
     * 根据id删除围墙
     * @param {*} id 标识Id
     * @param {*} isRemove 是否删除数据库数据
     */
    removeWall(id, isRemove) {
        isRemove = isRemove || false;
        let graphic = common.graphicLayer.getGraphicById(id);
        if (graphic) {
            common.graphicLayer.removeGraphic(graphic);
        }
        if (isRemove) {
            symbol.deleteSymbolAttribute([id]);
        }
    },

    /**
     * 删除所有围墙
     */
    clearAllWall(isRemove) {
        isRemove = isRemove || false;
        let ids = [];
        let graphics = common.graphicLayer.getGraphicsByAttr("name", "wall");
        for (let i = 0; i < graphics.length; i++) {
            common.graphicLayer.removeGraphic(graphics[i]);
            ids.push(graphics[i].id);
        }
        if (isRemove) {
            symbol.deleteSymbolAttribute(ids);
        }
    },
}


export default { wfs, symbol, divGraphic, pipe, road, wall, initLayer, getLayer, common };